<?php

namespace App\Admin\Controllers\Configs;

use App\Admin\Repositories\Domain;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class DomainController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
         return Grid::make(new Domain(), function (Grid $grid) {
             $grid->model()->orderBy('type')->orderBy('sort');
            $grid->column('id')->sortable();
            $grid->column('domain');
            $grid->column('type')->using([1 => '落地', 2 =>'主域名'])
            ->dot(
                [
                    1 => 'warning',
                    2 => 'success',
                ], 
                'primary' // 第二个参数为默认值
            );
            $grid->column('state','是否启用')
            ->if(function ($column) {
                if($column->getValue() == -1){
                    $column->using([-1 => '被封禁'])->label('danger');
                }
                else{
                    $column->switch();
                }
            })->sortable();
            $grid->column('mark')->editable();
            $grid->column('sort')->editable()->help('数字越小越靠前');
            // $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->enableDialogCreate();
            $grid->quickSearch(['domain']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('domain');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Domain(), function (Show $show) {
            $show->field('id');
            $show->field('domain');
            $show->field('type');
            $show->field('state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Domain(), function (Form $form) {
            $form->display('id');
            $form->text('domain')->required();
            $form->radio('type')->options([1 => '落地域名', 2=>'入口主域名'])->default(1);
            $form->switch('state')->options([1 => '启用', 0=> '停用'])->default(1);
            $form->number('sort')->default(1)->help('按数字排序,数字越小越靠前');
            $form->text('mark');
            $form->display('created_at');
            $form->display('updated_at');


            //编辑缓存同步
            $form->saved(function(Form $form){
                if($form->model()->type == 1){
                    cache()->forget('born_domain');
                }
                
            });

            $form->deleted(function (Form $form, $result) {
                // 获取待删除行数据，这里获取的是一个二维数组
                $data = $form->model()->toArray();
                
                // 通过 $result 可以判断数据是否删除成功
                if ( $result) {
                    cache()->forget('born_domain');
                    // 返回删除成功提醒，此处跳转参数无效
                    return $form->response()->success('删除成功');
                }
                return $form->response()->error('数据删除失败');
            });
        });
    }
}
